package com.dykj.fireFighting.web.base.maintenancePerson;

import java.util.*;

import com.dykj.fireFighting.api.base.maintenancePerson.dto.MaintenancePersonDto;
import com.dykj.fireFighting.api.base.maintenancePerson.entity.MaintenancePerson;
import com.dykj.fireFighting.api.base.maintenancePerson.service.IMaintenancePersonService;

import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 维保单位人员信息接口
 *
 * @Author JacksonTu
 * @Date  2021-11-03 09:08:51
 */
@Slf4j
@Api(value = "维保单位人员信息接口", tags = {"维保单位人员信息接口"})
@RestController
@RequestMapping("maintenancePerson")
public class MaintenancePersonController {

    @DubboReference
    private IMaintenancePersonService maintenancePersonService;


    /**
     * 维保单位人员信息列表
     *
     * @return
     */
    @AutoLog(value = "维保单位人员信息-分页列表查询")
    @ApiOperation(value = "维保单位人员信息分页列表", notes = "维保单位人员信息分页列表")
    @GetMapping("/list")
    @RequiresPermissions("maintenancePerson/list")
    public CommonResult<PageVo<MaintenancePerson>> treeGrid(MaintenancePersonDto maintenancePersonDto) {

        LoginUserVo user = ShiroUtil.getUser();
        maintenancePersonDto.setUnitId(user.getEnterpriseId().toString());
        PageVo<MaintenancePerson> page = maintenancePersonService.queryList(maintenancePersonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取维保单位人员信息信息
     */
    @AutoLog(value = "维保单位人员信息-获取维保单位人员信息信息")
    @ApiOperation(value = "获取维保单位人员信息信息", notes = "获取维保单位人员信息信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("maintenancePerson/info")
    public CommonResult<MaintenancePerson> info(@PathVariable("id") String id){
        MaintenancePerson maintenancePerson = maintenancePersonService.getInfoById(id);
        return CommonResult.success(maintenancePerson);
    }

    /**
     * 保存维保单位人员信息信息
     */
    @AutoLog(value = "维保单位人员信息-保存维保单位人员信息信息")
    @ApiOperation(value = "保存维保单位人员信息信息", notes = "保存维保单位人员信息信息")
    @PostMapping("/save")
    @RequiresPermissions("maintenancePerson/save")
    public CommonResult save(@Valid @RequestBody MaintenancePerson maintenancePerson) {
        try {
            LoginUserVo user = ShiroUtil.getUser();
            maintenancePerson.setCreateTime(new Date());
            maintenancePerson.setUnitId(user.getEnterpriseId().toString());
            maintenancePerson.setUnitName(user.getEnterpriseName());
            maintenancePerson.setCreateBy(user.getId().toString());
                maintenancePersonService.save(maintenancePerson);

            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改维保单位人员信息信息
     */
    @AutoLog(value = "维保单位人员信息-修改维保单位人员信息信息")
    @ApiOperation(value = "修改维保单位人员信息信息", notes = "修改维保单位人员信息信息")
    @PostMapping("/update")
    @RequiresPermissions("maintenancePerson/update")
    public CommonResult update(@Valid @RequestBody MaintenancePerson maintenancePerson) {
        try {
            LoginUserVo user = ShiroUtil.getUser();
            maintenancePerson.setUpdateBy(user.getId().toString());
            maintenancePerson.setUpdateTime(new Date());
                maintenancePersonService.updateById(maintenancePerson);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除维保单位人员信息信息
     */
    @AutoLog(value = "维保单位人员信息-删除维保单位人员信息信息")
    @ApiOperation(value = "删除维保单位人员信息信息", notes = "删除维保单位人员信息信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("maintenancePerson/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                maintenancePersonService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }


    /**
     * 社会单位下所有维保单位的用户信息  这个接口社会端用
     */
    @ApiOperation(value = "用户信息", notes = "用户信息")
    @GetMapping("/getUserBySocialUnit")
    public CommonResult getUserBySocialUnit(MaintenancePersonDto maintenancePersonDto) {

        LoginUserVo user = ShiroUtil.getUser();
        //这里是社会端id
        maintenancePersonDto.setUnitId(user.getEnterpriseId().toString());

        PageVo<MaintenancePerson> page =maintenancePersonService.getUserBySocialUnit(maintenancePersonDto);


        return CommonResult.success(page);
    }
}
